// index.js
// 获取应用实例
const app = getApp()

Page({
    data: {
        region: ['福建省', '厦门市', '思明区'],
        key:'758c89d2dc4e412ea6a9c87c53d9f8c2',//去官网复制你的key噢！
        locationId:'',
        weather:''
      },

      bindRegionChange: function (e) {
        console.log("我们获取到了用户的数据", e.detail.value)
        this.setData({
          region: e.detail.value
        })
        //获取到的地区赋值到data中的region后，调用getLocationId函数去请求getLocationId
        this.getLocationId()
      },

      getLocationId:function(){
        var that = this
        var key = this.data.key
        //调用该函数获取用户选的的地区
        wx.request({
            url: 'https://geoapi.qweather.com/v2/city/lookup?'+ 'location=' + this.data.region[2] + '&key=' + key,
          //如果请求成功
            success: function (res) {
              console.log('获取到了locationid啦',res.data.location[0].id)
              //把请求到的locationId赋值给data中的locationId
                that.setData({
                    locationId: res.data.location[0].id
                })
                
                //调用获取天气的函数
                that.getWeather()
            }
        })
      },

      getWeather:function(){
        //获取指针
        var that = this
        //这是我们的和风天气复制过来的key
        //再复习一下，我们请求天气需要什么？ 1.key，2.城市名字（城市名字是通过picker选择的）=> locationid
        var key = that.data.key
        var locationId = that.data.locationId
        console.log("太好啦，我们已经有key--->" + key)
        console.log("太好啦，我们已经有--->" + locationId)
        //请求天气数据
        wx.request({
          url: 'https://devapi.qweather.com/v7/weather/now?' + 'location=' + locationId + '&key=' + key,
          //如果请求成功
          success: function (res) {
            //把请求到的天气数据赋值给data中的weather
            that.setData({
              weather: res.data.now
            })
            console.log(res.data.now)
          }
        })
      },
      //页面加载
  onLoad:function(options){
    //每次打开页面调用这个函数然后获取从化区的天气
    this.getLocationId();
  },

  //用户分享
  onShareAppMessage: function () {
    return {
      title: '你好呀＾＾点击来查询实时天气吧~',
      path: '/pages/index/index'
    }
  }

})